<template>
    <rx-dialog @handOk="handleSel" @cancel="cancel">
      <span slot="toolbar">
       <a-checkbox v-if="mutilple == 1" :checked="!checkStrictly" @change="changeCheckStrictly(checkStrictly)">
        {{i18n('apiShow.checkStrictly','级联选择')}}
      </a-checkbox>
      <a-checkbox v-if="mutilple == 1" :checked="checkAll" @change="onChange">
        {{i18n('apiShow.checkAll','全选')}}
      </a-checkbox>
      </span>
        <a-form :form="form">
            <a-form-item style="display:none">
                <a-input v-decorator="['typeId']"/>
            </a-form-item>
            <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label=""
            >
                <a-tree v-if="mutilple == 1"
                        checkable
                        v-model="checkedKeys"
                        @check="onCheck"
                        :treeData="permissions"
                        :checkStrictly="checkStrictly"
                >
                </a-tree>
                <a-tree v-else
                        v-model="checkedKeys"
                        @select="handleSelect"
                        :treeData="permissions"
                >
                </a-tree>
            </a-form-item>
        </a-form>
    </rx-dialog>
</template>

<script>
import PublicApi from "@/api/user/org/public"
import {BaseForm, Util} from "@lowcode/jpaas-common-lib"

export default {
    name: "sysInterface",
    mixins: [BaseForm],
    props: {
        layerid: String,
        record: Object,
        destroy: Function,
        callback: {
            type: Function
        },
        lydata: {},
        mutilple: {},
        //仅选择子节点
        selectable: Boolean,
    },
    computed: {
        table() {
            return this.$refs.table;
        }
    },

    data() {
        return {
            allMenusList: [],
            permissions: [],
            checkedKeys: [],
            halfCheckedKeys: [],
            treeCheck: true,
            url: "",
            method: "",
            name: "",
            checkAll: false,
            checkStrictly: true,
            selectData:""
        }
    },

    mounted() {
        this.init();
    },

    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"sysMenu",key);
        },
        init() {
            var self_ = this;
            PublicApi.getAppServices().then(res => {
                res.forEach(item => {
                    var app = {title: item, key: item};
                    if (this.mutilple == 1) {
                        app = {title: item, key: (item + '$' + item)};
                    }
                    PublicApi.getAllClassByAppName(item).then(res => {
                        app.children = res;
                        res.forEach(item => {
                            self_.allMenusList.push(...item.children);

                            if (this.mutilple == 1) {
                                item.key = item.key + '$' + item.title;
                                this.dealChildren(item);
                            }

                        })
                        self_.permissions.push(app);
                    })
                })
            })
        },

        handleSelect(selectedKeys, info) {
            if(selectedKeys.length==0){
                this.selectData="";
                return;
            }
            var title = info.node.title;
            this.method = title.substring(1, title.indexOf(')'));
            this.name = title.substring(title.indexOf(')') + 1);
            this.url = selectedKeys[0];
            if(this.selectable){
                this.selectData=info.node.dataRef;
            }
        },
        handleSel(vm) {
            if (this.mutilple == 1) {
                let data = [], temp, groupMenu;
                this.checkedKeys.forEach(item => {
                    temp = item.substring(item.indexOf('$') + 1);
                    groupMenu = {
                        method: temp.substring(1, temp.indexOf(')')),
                        name: temp.substring(temp.indexOf(')') + 1),
                        url: item.substring(0, item.indexOf('$'))
                    }
                    data.push(groupMenu);
                });
                Util.closeWindow(this, "ok", data);
            } else {
                if(this.selectable){
                    if (!this.selectData) {
                        this.$message.warning(this.i18n('msa.batchChoiceTips','至少选择一个接口!'));
                        vm.loading=false
                        return;
                    }else if(!this.selectData.service ){
                        this.$message.warning(this.i18n('msa.batchChoiceTips2','请选择正确的接口!'));
                        vm.loading=false
                        return;
                    }
                    this.selectData.apiName=this.name;
                    this.selectData.url=this.url;
                    Util.closeWindow(this, "ok", this.selectData);
                }else {
                    let data = {name: this.name, url: this.url, method: this.method};
                    Util.closeWindow(this, "ok", data);
                }
            }
        },

        onCheck(checkedKeys, info) {
            if (!this.treeCheck) this.treeCheck = true;
            //非级联选择
            if (this.checkStrictly) {
                this.checkedKeys = checkedKeys.checked;
            } else {
                this.checkedKeys = checkedKeys;
            }
        },

        changeCheckStrictly(value) {
            this.checkStrictly = !value;
        },
        onChange(e) {
            this.checkAll = e.target.checked;
            this.checkedKeys = [];
            if (this.checkAll) {
                for (var i = 0; i < this.permissions.length; i++) {
                    this.checkedKeys.push(this.permissions[i].key);
                    this.pushChilden(this.permissions[i]);
                }
            }
        },
        pushChilden(menu) {
            if (menu.children) {
                for (var i = 0; i < menu.children.length; i++) {
                    this.checkedKeys.push(menu.children[i].key);
                    this.pushChilden(menu.children[i]);
                }
            }
        },
        dealChildren(menu) {
            if (menu.children) {
                for (var i = 0; i < menu.children.length; i++) {
                    menu.children[i].key = menu.children[i].key + '$' + menu.children[i].title;
                    this.dealChildren(menu.children[i]);
                }
            }
        }


    },
}
</script>